/**
 * @description: 获取数据类型
 * @param: (obj: any): any
 * @author: huen2015
 */

import { IFn } from './types'

function getType(obj: any): any {
	const type = Object.prototype.toString.call(obj) // '[object,Object]'
	return type.slice(8, -1).toLowerCase()
}

console.log('string:', getType('11'))
console.log('number:', getType(12))
console.log(
	'function',
	getType(() => {
		console.log('getType')
	}),
)
console.log('object', getType({}))
console.log('map', getType(new Map()))
console.log('set', getType(new Set()))
console.log('weakMap', getType(new WeakMap()))
console.log('weakSet', getType(new WeakSet()))
console.log('date', getType(new Date()))
console.log('regexp', getType(new RegExp('')))
console.log('null', getType(null))
console.log('undefined', getType(undefined))

/**
 * @description: 获取年月日
 * @param: (dTime: Date = new Date(), separation: string = '-'): string
 * @author: huen2015
 */
function getDate(dTime: Date = new Date(), separation = '-'): string {
	const year: number | string = dTime.getFullYear()
	let month: number | string = dTime.getMonth() + 1
	let date: number | string = dTime.getDate()

	if (month < 10) {
		month = '0' + month
	}

	if (date < 10) {
		date = '0' + date
	}

	return year + separation + month + separation + date
}

console.log('getDate', getDate())

/**
 * @description: 遍历数组和对象
 * @param:
 * @author: huen2015
 */

function forObj(obj: any[] | object, callback: IFn): any {
	if (Array.isArray(obj)) {
		obj.forEach((item: any, index: number) => {
			callback(item, index)
		})
	} else {
		Object.keys(obj).forEach((key: string, index: number) => {
			// @ts-ignore
			callback(obj[key], index)
		})
	}
}

const forArrayData = [1, 2, 3]
forObj(forArrayData, function (item: any, index: number) {
	console.log('forArrayDataResult', item, index)
})

const forObjData = {
	key: 'key',
	name: 'huen2015',
}
forObj(forObjData, function (item: any, index: number) {
	console.log('forObjDataResult', item, index)
})

/**
 * @description: 数组去重
 * @param:
 * @author: huen2015
 */

function unique(arr: any[]): any[] {
	return arr.filter((item: any, index: number) => {
		return arr.indexOf(item) === index
	})
}

const uniqueData = [1, 2, 3, 3, 2, 1]
const uniqueResult = unique(uniqueData)
console.log('uniqueResult', uniqueResult)

export { getType, getDate, forObj, unique }
